<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>JSReg</title>
		<style type="text/css">
			body {
				font: 62.5%/1.3em "Lucida Grande", verdana, arial, helvetica, sans-serif;
			}					
			#code, #converted, #result, #evalCode, #functionCode, #globalsCheck, #errorLog, #parseTree, #source {
				width:500px;
				height:200px;				
			}
			#source {
				overflow:auto;
			}
			#evalCode, #functionCode {
				height:90px;
			}
			label {
				display:block;
			}
			.column {
				float:left;
				margin-right:10px;
			}
			.clear {
				clear:both;
			}							
		</style>		
		<script type="text/javascript" src="JSReg.js"></script>
		<script type="text/javascript">		
		function checkGlobals(parser){
				var obj = {};
				var globals = [];
				var frame = document.body.appendChild(document.createElement("frame"));
				for (var i in frame.contentWindow) {
					obj[i] = true;
				}
				document.body.removeChild(frame);
				for (var i in window) {
					if (i === 'GetWeakReference') {
						continue;
					}
					if (!obj[i]) {
						globals.push(i);
					}
				}
				
				for(var i in Array) {
					globals.push('Array.'+i);
				}
				if (parser.debugObjects.globalsCheck) {
					parser.debugObjects.globalsCheck(globals.join(","));
				}
				frame = null;
		}				
		function errorHandler(e, parser) {
			if (e.description) {
					var msg = '';
					if (e.description) {
						msg += 'Description:' + e.description + '\n';
					}
					if(e.msg) {
						msg += 'Msg:'+e.msg+'\n';
					}
					if (e.line) {
						msg += 'Line:' + e.line+'\n';
					}										
										
					if (parser.debugObjects.parseTree) {
						parser.debugObjects.parseTree(e.parseTree);
					}					
					if (parser.debugObjects.errorLog) {
						parser.debugObjects.errorLog(msg);
					} else {												
						alert(msg);
					}
				} else {
					if (parser.debugObjects && parser.debugObjects.errorLog) {
						parser.debugObjects.errorLog(e);
					} else {						
						alert(e);
					}
				}			
		}		
		function execJSREG(){
					var parser = JSReg.create();											
					parser.setDebugObjects({functionCode: function(code) {
						document.getElementById('functionCode').value = code;
					},
					evalCode: function(code) {
						document.getElementById('evalCode').value = code;
					},										
					converted: function(code) {
						document.getElementById('converted').value = code;
					},
					result: function(code){
						document.getElementById('result').value = 'type:'+typeof code+'\n'+code;
					},
					globalsCheck: function(code){
						document.getElementById('globalsCheck').value = code;
					},
					errorLog: function(code) {
						document.getElementById('errorLog').value += code + '\n';
					},
					parseTree: function(code) {
						document.getElementById('parseTree').value += code.join("\n") + '\n';
					},
					clearTree: function() {
						document.getElementById('parseTree').value = '';
					},
					errorHandler: function(e) {
						errorHandler(e, parser);
					},
					onComplete:function() {							
						document.body.removeChild(parser.environment);																					
						checkGlobals(parser);
						parser.environment = null;
						parser = null;																		
					},
					onStart:function() {																														
						checkGlobals(parser);
					},																		
					doNotMainEval: false,
					doNotEval: false,
					doNotFunctionEval: false																											
				   });
 			try {
                   throw new Error()
                } 
                catch (e) {
                    var relativeLineNumber = e.lineNumber
            }
							   			
			try {										
				var code = document.getElementById('code').value;																																																																																									
				parser.eval(code);																																											
			} 
			catch (e) {					
                line = (e.lineNumber - relativeLineNumber - 1);
                e.line = line;
				errorHandler(e, parser);
			}			
		}
		document.onkeydown = function(e) {
			e = window.event ? event : e; 
			if(e.keyCode==13&&e.ctrlKey==true){
				execJSREG()
			}
		}
		</script>
	</head>
	<body>
		
		<img src="images/logo.gif" />
		
		<form>
		<fieldset>
		<legend>JSReg</legend>				
		<p class="clear">
			<span class="column">
				<label>Your Code:</label>
				<textarea id="code"></textarea>
			</span>
			<span class="column">
				<label>JSReg Code:</label>
				<textarea id="converted"></textarea>			
			</span>
		</p>
		<p class="clear">
		<span class="column">
			<label>globals.eval code:</label>
			<textarea id="evalCode"></textarea>			
			<label>globals.Function code:</label>
			<textarea id="functionCode"></textarea>			
		</span>
		</p>								
		<span class="column">
		<p class="clear">
			<label>Result:</label>
			<textarea id="result"></textarea>
		</p>
		</span>
		<p class="clear">
			<input type="reset" value="Clear" /> 
			<input type="button" value="Clear errors" onclick="document.getElementById('errorLog').value=''" /> 			
			<input type="button" value="Execute code" onclick="execJSREG()" accesskey="X" />
		</p>
		<p class="clear">				
		<span class="column">				
			<label>Globals check:</label>
			<textarea id="globalsCheck"></textarea>	
		</span>				
		<span class="column">
			<label>Error log:</label>
			<textarea id="errorLog"></textarea>						
		</span>
		</p>	
		<p class="clear">
			<span class="column">
				<label>Parse tree:</label>
				<textarea id="parseTree"></textarea>
			</span>			
		</p>				
		</fieldset>					
		</form>					
	</body>
</html>
